package Thread;

import Logcheck.CheckConstant;
import Logcheck.User_log;
import Logcheck.User_signup_check;
import MessageOp.GetMessage;
import UserFriend.Friend_check;
import MessageOp.SendMessage;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class MyFunction implements Runnable
{
    private DatagramPacket receivePacket;
    private DatagramSocket receiveSocket;
    private JSONObject snedJson;

    public MyFunction(DatagramPacket datagramPacket,DatagramSocket receivesocket)
    {
        receivePacket = datagramPacket;
        receiveSocket = receivesocket;
    }

    public void send()
    {
        try
        {
            byte[] sendData = snedJson.toString().getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receivePacket.getAddress(), receivePacket.getPort());

            receiveSocket.send(sendPacket);

            // 打印发送的数据
            System.out.printf("\n[%s][UDP][端口:%d]向服务器[%s]发送数据：\n%s\n",UDPServer.getTime(), receivePacket.getPort(), receivePacket.getAddress(), snedJson.toString(JSONWriter.Feature.PrettyFormat));

        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public void send_to_friend()
    {

    }

    @Override
    public void run()
    {
        //System.out.print("这是断点："+new String(receivePacket.getData(),0, receivePacket.getLength()));
        JSONObject responseJson = JSON.parseObject(new String(receivePacket.getData()));
        //System.out.print(responseJson.toString());
        String function = responseJson.getString("function");

        if(function==null)
        {
            System.out.print("function is empty");
            return;
        }

        switch (function)
        {
            case "checkUsername":
                snedJson = User_signup_check.checkUsername(receivePacket);
                send();
                break;
            case "signUp":
                snedJson = User_log.User_SignUp(receivePacket);
                send();
                break;
            case "logIn":
                snedJson = User_log.logIn(receivePacket);
                send();
                break;
            case "searchUser":
                snedJson = Friend_check.friend_search(receivePacket);
                send();
                break;
            case "sendMessage":
                SendMessage.sendMessage(receivePacket);
                break;
            case "getMessage":
                snedJson = GetMessage.getMessage(receivePacket);
                if(snedJson.getInteger("code")== CheckConstant.True)
                {
                    send();
                }
                break;
            case "agreeFriendApply":
                Friend_check.friend_add(receivePacket);
                break;
            case "getFriendList":
                snedJson = Friend_check.get_friend_list(receivePacket);
                send();
                break;
            default:
                System.out.print("");
                send();
                break;
        }
    }
}
